limsExcelReadconfig.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-excel-readconfig-edit-list-grid',
            url : limsExcelReadconfig.baseUrl + '/queries',
            service : 'limsExcelReadconfigServiceImpl',
            dbTable : 'T_LIMS_EXCEL_READCONFIG',
            columns : limsExcelReadconfig.getEditListColumns(),
            deleteFormatter: function (row) {
                return row.name || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.loadRightData(-1);
                }
            },
            onRowActive : function(index, row) {
                _this.loadRightData(row.id);
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'BASELINE.MODULE.EXCELREADS.MODAL.ADD',
                        fields : limsExcelReadconfig.getBaseInfoFormFields(),
                        url : limsExcelReadconfig.baseUrl
                    }).done(function(id) {
                        Gikam.getComp('lims-excel-readconfig-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-excel-readconfig-edit-list-grid').deleteRows(limsExcelReadconfig.baseUrl);
                }
            } , {
                type : 'button',
                text : '测试解析',
                icon : 'add',
                onClick : function() {
                    _this.testExcelParse();
                }
            } ]
        }
    },
    testExcelParse : function(){
        var _this = this;
        var grid = Gikam.getComp('lims-excel-readconfig-edit-list-grid');
        var activedRow = grid.getActivedRow();
        if (Gikam.isEmpty(activedRow)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }

        //上传文件
        Gikam.create('simpleUploader', {
            id: 'temp-excel',
            dbTable: 'T_LIMS_EXCEL_READCONFIG',
            bizId : activedRow.id,
            autoClose: true,
            multiple: false,
            onUploadSuccess: function (fileList) {

                workspace.window.showMask();
                Gikam.post(limsExcelReadconfig.baseUrl + '/excel/testparse',
                    Gikam.getJsonWrapper({
                        fileId : fileList[0].id
                    }, [null, [activedRow]])).done(function (datas) {
                    var simpleModal = Gikam.createSimpleModal({
                        width: '80%',
                        height: '80%',
                        title : '解析结果',
                        showMaxBtn: false,
                        bodyStyle: {
                            padding: "0 16px"
                        },
                        toolbarFormatter: function () {
                            return [ {
                                type: "button",
                                text: "GIKAM.BUTTON.CLOSE",
                                class: "red",
                                onClick: function () {
                                    Gikam.getLastModal().close()
                                }
                            }];
                        }
                    });
                    //每个要解析的sheet后台的结果
                    var panels = [];
                    for (var i = 0; i < datas.length; i++) {
                        var line = '';
                        for (var j = 0; j < datas[i].rowDataList.length; j++) {
                            line = line + JSON.stringify(datas[i].rowDataList[j])+'\n';
                        }
                        panels.push({
                            title : datas[i].sheetName,
                            items : [
                                {
                                    type : 'form',
                                    width : 1000,
                                    fields : [
                                        {
                                            field : 'content',
                                            type : 'textarea',
                                            autoSave : false,
                                            height : 800,
                                            value : line
                                        }
                                    ]
                                }
                            ]
                        });
                    }
                    var form = Gikam.create('tab', {
                        panels : panels,
                        renderTo: simpleModal.layout.options.center.$dom
                    });
                }).fail(function(){
                }).always(function () {
                    workspace.window.closeMask();
                });
            }
        });
    },
    loadRightData : function(id){
        var _this = this;
        Gikam.getAsyncComp('lims-sheet-readconfig-edit-list-grid').done(function(comp) {
            comp.refresh({
                url : limsSheetReadconfig.baseUrl + '/queries',
                requestData : {
                    excelId_SEQ : id
                },
            });
        });
    },

    getSheetConfigGrid : function(){
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-sheet-readconfig-edit-list-grid',
            service : 'limsSheetReadconfigServiceImpl',
            dbTable : 'T_LIMS_SHEET_READCONFIG',
            columns : limsSheetReadconfig.getEditListColumns(),
            deleteFormatter: function (row) {
                return row.sheetName || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.loadBelowData(-1);
                }
            },
            onRowActive : function(index, row) {
                _this.loadBelowData(row.id);
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var grid = Gikam.getComp('lims-excel-readconfig-edit-list-grid');
                    var activedRow = grid.getActivedRow();
                    if(Gikam.isEmpty(activedRow)){
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.preInsert({
                        modalTitle : 'BASELINE.MODULE.EXCELREADS.MODAL.ADD',
                        fields : limsSheetReadconfig.getBaseInfoFormFields(),
                        url : limsSheetReadconfig.baseUrl,
                        param : {
                            excelId : activedRow.id,
                        }
                    }).done(function(id) {
                        Gikam.getComp('lims-sheet-readconfig-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-sheet-readconfig-edit-list-grid').deleteRows(limsSheetReadconfig.baseUrl);
                }
            } ]
        }
    },
    loadBelowData : function(id){
        var _this = this;
        Gikam.getAsyncComp('lims-sheet-title-config-edit-list-grid').done(function(comp) {
            comp.refresh({
                url : limsSheetTitleConfig.baseUrl + '/queries',
                requestData : {
                    sheetId_SEQ : id
                },
            });
        });
    },
    getSheetTitleConfigGrid : function(){
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-sheet-title-config-edit-list-grid',
            service : 'limsSheetTitleConfigServiceImpl',
            dbTable : 'T_LIMS_SHEET_TITLE_CONFIG',
            deleteFormatter: function (row) {
                return row.title || row.id;
            },
            columns : limsSheetTitleConfig.getEditListColumns(),
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var grid = Gikam.getComp('lims-sheet-readconfig-edit-list-grid');
                    var activedRow = grid.getActivedRow();
                    if(Gikam.isEmpty(activedRow)){
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }

                    Gikam.preInsert({
                        modalTitle : 'BASELINE.MODULE.EXCELREADS.MODAL.ADD',
                        fields : limsSheetTitleConfig.getBaseInfoFormFields(),
                        url : limsSheetTitleConfig.baseUrl,
                        param : {
                            sheetId : activedRow.id
                        }
                    }).done(function(id) {
                        Gikam.getComp('lims-sheet-title-config-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-sheet-title-config-edit-list-grid').deleteRows(limsSheetTitleConfig.baseUrl);
                }
            } ]
        }
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-excel-readconfig-edit-list-layout',
            renderTo : workspace.window.$dom,
            center : {
                items : [ this.getGridParam() ]
            },
            east : {
                width :  '65%',
                items : [
                    _this.getSheetConfigGrid(),
                    _this.getSheetTitleConfigGrid(),
                ]
            }
        });
    },

    init : function() {
        this.create();
    }
};
